﻿var Resource_Personal = {
    txtFileName_init: function () {
        $("#txtFileName").mousedown(function () {
            if ($(this).val() == "Tên file") {
                $(this).css("color", "#000");
                $(this).val("");
            }
        });

        $("#txtFileName").blur(function () {
            if ($.trim($(this).val()) == "") {
                $(this).css("color", "#999");
                $(this).val("Tên file");
            }
        });

        $("#txtFileName").keyup(function (e) {
            if (e.keyCode == 13) {
                var categoryid = $("#ddlCategoryID").val();
                var filename = $("#txtFileName").val();
                if ($.trim(filename) == "Tên file") filename = "";
                GetPersonalFiles(1, categoryid, filename);
            }
        });
    },

    pager: function () {
        $("ul.pager li a").click(function () {
            var page = $(this).attr("page");
            GetPersonalFiles(page);
        });
    },

    search: function () {
        $("#btnSearchFiles").click(function () {
            var categoryid = $("#ddlCategoryID").val();
            var filename = $("#txtFileName").val();
            if ($.trim(filename) == "Tên file") filename = "";
            GetPersonalFiles(1, categoryid, filename);
        });
    },

    UpdateSharing: function () {
        InitUpdateSharingEvent();
    },

    Delete: function () {
        InitDeletePersonalFile();
    },

    init: function () {
        Resource_Personal.txtFileName_init();
        Resource_Personal.pager();
        Resource_Personal.search();
        Resource_Personal.UpdateSharing();
        Resource_Personal.Delete();
    }
}

var Resource_Community = {
    txtFileName_init: function () {
        $("#txtFileName").mousedown(function () {
            if ($(this).val() == "Tên file") {
                $(this).css("color", "#000");
                $(this).val("");
            }
        });

        $("#txtFileName").blur(function () {
            if ($.trim($(this).val()) == "") {
                $(this).css("color", "#999");
                $(this).val("Tên file");
            }
        });

        $("#txtFileName").keyup(function (e) {
            if (e.keyCode == 13) {
                var categoryid = $("#ddlCategoryID").val();
                var filename = $("#txtFileName").val();
                if ($.trim(filename) == "Tên file") filename = "";
                GetCommunityFiles(1, categoryid, filename);
            }
        });
    },

    pager: function () {
        $("ul.pager li a").click(function () {
            var page = $(this).attr("page");
            GetCommunityFiles(page);
        });
    },

    search: function () {
        $("#btnSearchFiles").click(function () {
            var categoryid = $("#ddlCategoryID").val();
            var filename = $("#txtFileName").val();
            if ($.trim(filename) == "Tên file") filename = "";
            GetCommunityFiles(1, categoryid, filename);
        });
    },

    init: function () {
        Resource_Community.txtFileName_init();
        Resource_Community.pager();
        Resource_Community.search();
    }
}

function GetPersonalFiles(page)
{
    $("div#loading").css("width", $("div#loading").parent().width());
    $("div#loading").css("height", $("div#loading").parent().height());	
	$("div#loading").css("display", "block");
	$.ajax({
	    url: "/Resource/GetPersonalFiles",
	    type: "GET",
	    data: { "page": page },
	    cache: false,
	    dataType: "html",
	    success: function (data) {
	        $("div#tableData").html(data);
	        $("ul.pager li a").click(function () {
	            GetPersonalFiles($(this).attr("page"));
	        });

	        InitUpdateSharingEvent();
	        InitDeletePersonalFile();
	        $("div#loading").css("display", "none");	       
	    }
	});
}

function GetPersonalFiles(page, categoryid, filename) {
    $("div#loading").css("width", $("div#loading").parent().width());
    $("div#loading").css("height", $("div#loading").parent().height());
    $("div#loading").css("display", "block");
    $.ajax({
        url: "/Resource/GetPersonalFiles",
        type: "GET",
        data: { "page": page, "categoryID": categoryid, "fileName": filename },
        cache: false,
        dataType: "html",
        success: function (data) {
            $("div#tableData").html(data);
            $("ul.pager li a").click(function () {
                GetPersonalFiles($(this).attr("page"), categoryid, filename);
            });

            InitUpdateSharingEvent();
            InitDeletePersonalFile();
            $("div#loading").css("display", "none");            
        }
    });
}

function InitUpdateSharingEvent() {
    $("span[title='Private'][active='false'], span[title='Friends'][active='false'], span[title='Public'][active='false']").click(function () {
        $("div#loading").css("width", $("div#loading").parent().width());
        $("div#loading").css("height", $("div#loading").parent().height());
        $("div#loading").css("display", "block");
        var fileID = $(this).parent().parent().attr("id");
        var share = $(this).attr("title");
        var obj = $(this);

        $.ajax({
            url: "/Resource/UpdateSharing",
            type: "POST",
            data: { "fileID": fileID, "share": share },
            cache: false,
            dataType: "html",
            success: function (data) {
                if (data == "0") {
                    var curActive = $(obj).parent().find("span[active='true']");
                    $(curActive).removeClass("active");
                    $(curActive).attr("active", "false");
                    $(obj).addClass("active");
                    $(obj).attr("active", "true");

                    InitUpdateSharingEvent();
                }
                else {
                    alert(data);
                }

                $("div#loading").css("display", "none");
            }
        });
    });
}

function InitDeletePersonalFile() {
    $("span.delete_icon16").click(function () {
        if (confirm("Bạn muốn xóa tài nguyên này ?") == true) {
            $("div#loading").css("width", $("div#loading").parent().width());
            $("div#loading").css("height", $("div#loading").parent().height());
            $("div#loading").css("display", "block");
            var fileID = $(this).parent().parent().attr("id");
            var deletedRow = $(this).parent().parent();
            $.ajax({
                url: "/Resource/DeleteFile",
                type: "POST",
                data: { "fileID": fileID },
                cache: false,
                dataType: "html",
                success: function (data) {
                    if (data == "0") {
                        $(deletedRow).fadeOut('slow', function () {
                            var parentOfDeletedRow = $(deletedRow).parent();
                            $(deletedRow).remove();
                            var files = $(parentOfDeletedRow).find("tr").size();
                            if (files == 0) {
                                // List files is empty now
                                var curPage = $("ul.pager li.current").html();
                                if (curPage > 1) {
                                    $("ul.pager li a[page='" + (curPage - 1) + "']").click();
                                }                                
                            }
                        });
                    }
                    else {
                        alert(data);
                    }

                    $("div#loading").css("display", "none");
                }
            });
        }
    });
}

function GetCommunityFiles(page) {
    $("div#loading").css("width", $("div#loading").parent().width());
    $("div#loading").css("height", $("div#loading").parent().height());
    $("div#loading").css("display", "block");
    $.ajax({
        url: "/Resource/GetCommunityFiles",
        type: "GET",
        data: { "page": page },
        cache: false,
        dataType: "html",
        success: function (data) {
            $("div#tableData").html(data);
            $("ul.pager li a").click(function () {
                GetCommunityFiles($(this).attr("page"));
            });

            $("div#loading").css("display", "none");
        }
    });
}

function GetCommunityFiles(page, categoryid, filename) {
    $("div#loading").css("width", $("div#loading").parent().width());
    $("div#loading").css("height", $("div#loading").parent().height());
    $("div#loading").css("display", "block");
    $.ajax({
        url: "/Resource/GetCommunityFiles",
        type: "GET",
        data: { "page": page, "categoryID": categoryid, "fileName": filename },
        cache: false,
        dataType: "html",
        success: function (data) {
            $("div#tableData").html(data);
            $("ul.pager li a").click(function () {
                GetCommunityFiles($(this).attr("page"), categoryid, filename);
            });

            $("div#loading").css("display", "none");
        }
    });
}